package org.lifei.util;

import com.baidu.aip.contentcensor.AipContentCensor;
import com.baidu.aip.contentcensor.EImgType;
import org.json.JSONException;
import org.json.JSONObject;

public class BaiduAiUtils {
    //设置APPID/AK/SK
    public static final String APP_ID = "52506311";
    public static final String API_KEY = "JHDnrIUPDJbI8qzSVR8nSr1r";
    public static final String SECRET_KEY = "ewoXPL4yUWw8Rl3xTRcoAMPJxjFKHpgI";
    private static AipContentCensor client;
    static {
        // 初始化一个AipContentCensor
         client = new AipContentCensor(APP_ID, API_KEY, SECRET_KEY);

        // 可选：设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
    }

    /**
     * 要审核的文本
     * @Return 审核结果 false不合理 true合理
     * @param text
     * @throws Exception
     */
    public static boolean textReview(String text){

        // 参数为待审核的文本字符串
        try {
            JSONObject response = client.textCensorUserDefined(text);
            int conclusionType = response.getInt("conclusionType");
            return  conclusionType==1? true:false;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 要审核的图像地址
     * @param url
     * @return
     */
    public static boolean imageReview(String url){

        // 参数为待审核的文本字符串
        try {
            JSONObject response = client.imageCensorUserDefined(url, EImgType.URL, null);
            int conclusionType = response.getInt("conclusionType");
            System.out.println(response.toString());
            return  conclusionType==1? true:false;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static void main(String[] args) {
        imageReview("\thttps://img14.360buyimg.com/n7/jfs/t1/232050/37/11739/87771/65978409F402afcd6/c0b83949abac1797.jpg");
    }
}